/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;

/**
 * <p>Project: app2022 - Ex10
 * <p>Powered by webrx On 2022-01-20 09:05:15
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex10 {
    public static int num = 0;
    public static int num1 = 0;

    public static void main(String[] args) {
        count(new File("D:\\jdk\\jdk-17.0.1\\lib\\src"));
        System.out.println(num);
        System.out.println(num1);

    }

    public static void count(File file) {
        for (File t : file.listFiles()) {
            if (t.isDirectory()) {
                count(t);
            } else {
                isjava(t);
            }
        }
    }

    public static void isjava(File file) {
        if (file.exists()) {
            if (file.getName().lastIndexOf(".java") != -1) {
                num++;
                //try (var fis = new FileInputStream(file);
//                     var fis1 = new FileInputStream(file);
//                     var fos = new FileOutputStream(new File("F:/jdk17_src.txt"), true)) {
//                    fos.write(String.format("%n-----------------------------------------------------").getBytes());
//                    fos.write(String.format("%n文件名：%s", file.getName()).getBytes());
//                    fos.write(String.format("%n文件路径：%s", file.getAbsolutePath()).getBytes());
//                    fos.write(String.format("%n-----------------------------------------------------%n").getBytes());
//                    fis.transferTo(fos);
                try (var br = new BufferedReader(new FileReader(file)); var bw = new BufferedWriter(new FileWriter("d:\\jdk17_src.txt", true))) {
                    bw.write(String.format("%n-----------------------------------------------------"));
                    bw.write(String.format("%n文件名：%s", file.getName()));
                    bw.write(String.format("%n文件路径：%s", file.getAbsolutePath()));
                    bw.write(String.format("%n-----------------------------------------------------%n"));
                    while (br.ready()) {
                        num1++;
                        bw.write(String.format("%n") + br.readLine());
                    }
                    bw.write(String.format("%n%n"));
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }
    }
}
